package com.lgd.factoryattendance.Activity;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lgd.factoryattendance.Bean.SysUser;
import com.lgd.factoryattendance.R;
import com.lgd.factoryattendance.Utils.ToastUtils;
import com.lgd.factoryattendance.httpService.API;
import com.lgd.factoryattendance.httpService.httpPort.IStringResponse;

import java.util.List;

public class StartActivity extends AppCompatActivity implements View.OnClickListener{

    SharedPreferences sp;
    SharedPreferences.Editor editor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        initView();
    }
    private void initView() {
        sp =getSharedPreferences("device",MODE_PRIVATE);
        editor = sp.edit();
        // 查找 admin  userid = 1 信息
//        SysUser sysUser = new SysUser();
//        sysUser.setUserId(1L);
//        seleceStaffInfo(sysUser);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(StartActivity.this,HomePageActivity.class));
                finish();
            }
        },1000);
    }

    // 查询 管理员 卡号信息
    public void seleceStaffInfo(SysUser sysUser){
        API.seleceStaffInfo(sysUser, new IStringResponse() {
            @Override
            public void onSuccess(String result) {
                StartActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        JSONObject jsonObject = JSONObject.parseObject(result);
                        List<SysUser> sysUserList = new Gson().fromJson(jsonObject.getString("data"), new TypeToken<List<SysUser>>(){}.getType());
                        if (sysUserList != null){
                            String icCard = sysUserList.get(0).getIcCard();
                            editor.putString("idCard",icCard);
                            editor.apply();
                        }else {
                            ToastUtils.warnShowLong(getApplicationContext(),"管理员卡号信息获取失败！！！");
                        }
                    }
                });
            }
            @Override
            public void onFail(int statusCode) {}
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){}
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}